<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <!-- 把 addTodo方法传给了 MyHeader -->
        <MyHeader @addTodo="addTodo"></MyHeader>
        <MyList :todoArr="todos"></MyList>
        <MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"></MyFooter>
      </div>
    </div>


    <!-- 动画效果demo -->

    <!-- 1个动画效果 -->
    <!-- <DongHuaTest></DongHuaTest> -->
    <!-- 多个动画效果 -->
    <!-- <GuoDuTest></GuoDuTest> -->
    <!-- 使用第三方css库 animate.css -->
    <!-- <ImportDongHua></ImportDongHua> -->
  </div>
</template>

<script>
import MyHeader from './components/MyHeader.vue'
import MyList from './components/MyList.vue'
import MyFooter from './components/MyFooter.vue'

import DongHuaTest from './components/DongHuaTest'
import GuoDuTest from './components/GuoDuTest'
import ImportDongHua from './components/ImportDongHua'

export default {
  name: 'App',
  components: {
    MyHeader,
    MyList,
    MyFooter,
    DongHuaTest,
    GuoDuTest,
    ImportDongHua
  },
  data() {
    return {
      todos: JSON.parse(localStorage.getItem('todos')) || []
    }
  },
  methods: {
    // 添加一个todo
    addTodo(todoObj) {
      // 接收到新加的对象，添加到todos数组的最前面
      this.todos.unshift(todoObj)
    },
    // 勾选或者取消勾选一个todo
    checkTodo(id) {
      this.todos.forEach((todo) => {
        // 找到页面上对应的todo对象，修改todo对象的done属性
        if (todo.id === id) {
          todo.done = !todo.done
        }
      })
    },
    updateTodo(id, title) {
      this.todos.forEach((todo) => {
        // 找到页面上对应的todo对象，赋值todo对象
        if (todo.id === id) {
          todo.title = title
        }
      })
    },
    // 删除一个todo
    deleteTodo(id) {
      // 只显示不是要删除的todo的id的，那些todo的对象
      this.todos = this.todos.filter(todo => todo.id !== id)
    },
    // 全选 或 全不选
    checkAllTodo(done) {
      // 所有todo对象都赋值一遍 done
      this.todos.forEach(todo => {
        todo.done = done
      });
    },
    clearAllTodo() {
      // 只赋值所有的 done 为 false 的 todo对象，
      // 因为已经选中的不可以再次取反，那就相当于把已选的又取消了，这是要全选的操作
      this.todos = this.todos.filter(todo => !todo.done)
    }
  },
  // 事件监听 todos 发生改变时，触发
  watch: {
    // 完整写法的事件监听
    todos: {
      deep: true,   // 开启深度监视
      handler(value) {
        localStorage.setItem('todos', JSON.stringify(value))
      }
    }
  },
  // 使用消息总线接收
  mounted() {
    this.$bus.$on('checkTodo', this.checkTodo)
    this.$bus.$on('deleteTodo', this.deleteTodo)
    this.$bus.$on('updateTodo', this.updateTodo)
  },
  // 不要忘记解绑消息总线绑定
  beforeDestroy() {
    this.$bus.$off('checkTodo', this.checkTodo)
    this.$bus.$off('deleteTodo', this.deleteTodo)
    this.$bus.$off('updateTodo', this.updateTodo)
  }

}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-edit {
  color: #fff;
  background-color: skyblue;
  border: 1px solid rgb(99, 148, 167);
  margin-right: 5px;
}


.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>


